﻿/*
NOTE:
cache.loadImage(_url:String)				** use to load image
cache.loadImageByArray(arr:Array)			** extend function for load a list of image, one by one.
----------
dispatchEvent:
	LoadEvent
Util tools:
	cache.isFinish							** return False when loading[each image]
	cache.IMG								** return MovieClip>Bitmap current IMAGE loaded
	cache.IMGArray							** return ImageHolder
	cache.releaseRAM						** delete ImageHolder, should be CPU resocurcse.
*/
package com.cf.core.Utils{
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.display.Bitmap;
	import flash.display.Sprite;
	import flash.display.*;
	import flash.events.EventDispatcher;
	import flash.display.Loader;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import com.cf.core.events.LoadEvent;
	public class cacheIMG extends EventDispatcher{
		private var __inUse:Boolean=false;
		public function get inUse():Boolean{return __inUse}
		public function set inUse(_f:Boolean):void{__inUse=_f}
		private var __ImageURLArray:Array=new Array;
		private var __ImageHolder:Array=new Array;
		private var __currURL:String;
		private var __loader:Loader;
		public function loadImageByArray(arr:Array):void {
			if (arr.length!=0 && !inUse) {
				releaseRAM();
				__ImageURLArray=arr;
				loadImage(__ImageURLArray[0]);
				inUse=true;
			}//if
		}//fn
		
		public function loadImage(_url:String):void {
			//Create the loader
			__loader = new Loader();
			//Get the URL to load
			var urlRequest:URLRequest=new URLRequest(_url);
			//Remove the first URL in the imageURLs array since we
			//started to load that image.
			__currURL = __ImageURLArray.shift();
			//Listen when the loading is complete
			__loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, handleIoError, false, 0, true);
			__loader.contentLoaderInfo.addEventListener(Event.COMPLETE, handleImageLoaded, false, 0, true);
			//Load the image
			__loader.load(urlRequest);
			dispatchEvent(new LoadEvent(LoadEvent.BEFORELOAD, {url: __currURL}));
		}//fn
		
		private function handleIoError(e:IOErrorEvent):void{
			trace("Loader URL : "+__currURL+" didn't exist");
			// put a empty Sprite inside.
			__ImageHolder[__currURL] = Sprite(undefined);
			dispatchEvent(new LoadEvent(LoadEvent.FAIL, {url: __currURL}));
			nexturl();
		}//fn
		
		private function handleImageLoaded(e:Event):void {
			inUse=false;
			if(e.target.content is Bitmap){
				//Save the loaded bitmap to a local variable
				var image:Bitmap = Bitmap(e.target.content);
				//Save the image's holder to a local variable
				//repackage in a MovieClip
				__ImageHolder[__currURL]= new MovieClip;
				__ImageHolder[__currURL].addChild(image);
				// trace("Loaded URL : "+ __currURL + " is "+ image);
				image=undefined;
			}else{
				trace("Loaded Unknow filetype : "+ typeof(e.target.content));
			}//if
			// Keep loading another
			__loader.contentLoaderInfo.removeEventListener(IOErrorEvent.IO_ERROR, handleIoError);
			__loader.contentLoaderInfo.removeEventListener(Event.COMPLETE, handleImageLoaded);
			__loader=new Loader();
			dispatchEvent(new LoadEvent(LoadEvent.ONLOAD, {url: __currURL}));
			nexturl();
		}//fn
		
		private function nexturl():void{
			inUse=false;
			// Keep loading another
			if(__ImageURLArray.length>0) loadImageByArray(__ImageURLArray);
		}//fn
		
		//////////////////////////////
		// I/O port
		//////////////////////////////
		public function isFinish():Boolean{
			var msg:String=new String;
			if(!inUse && __ImageURLArray.length==0){
				return true;
			}else
				return false;
		}//fn
		public function get IMG():MovieClip{
			return __ImageHolder[__currURL] as MovieClip;
		}//fn
		public function get IMGArray():Array{
			return __ImageHolder;
		}//fn
		public function releaseRAM():void{
			RELEASE();
		}//fn
		public function RELEASE():Boolean{
			if(!inUse){
				__ImageURLArray = new Array();
				__ImageHolder = new Array();
				dispatchEvent(new LoadEvent(LoadEvent.RELEASE, {url: __currURL}));
				return true;
			}else{
				trace("Loader"+typeof(this)+" ,still working");
				return false;
			}//fn
		}//fn
	}//class
}//package